// 导入Tauri的invoke函数用于调用后端命令
import { invoke } from '@tauri-apps/api/core';

// 定义UDP配置接口
interface UdpConfig {
    host: string;
    port: number;
}

// 定义播放场景命令接口
interface PlaySceneCommand {
    scene_number: number;
}

// 定义一个UDP客户端类，用于发送播放和结束播放命令
export class UdpClient {
    private port: number;
    private host: string;

    // 构造函数初始化端口和主机地址
    constructor(port: number, host: string) {
        this.port = port;
        this.host = host;
    }

    // 播放指定场景的方法
    public async playScene(sceneNumber: number): Promise<void> {
        try {
            const config: UdpConfig = {
                host: this.host,
                port: this.port
            };
            
            const command: PlaySceneCommand = {
                scene_number: sceneNumber
            };
            
            const result = await invoke<string>('play_scene', { config, command });
            console.log(result);
        } catch (error) {
            console.error('Failed to play scene:', error);
            throw error;
        }
    }

    // 结束播放的方法
    public async stopPlaying(): Promise<void> {
        try {
            const config: UdpConfig = {
                host: this.host,
                port: this.port
            };
            
            const result = await invoke<string>('stop_playing', { config });
            console.log(result);
        } catch (error) {
            console.error('Failed to stop playing:', error);
            throw error;
        }
    }
}
